/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.*;

/**
 *
 * @author linux
 */
public class ConnectionSigleton {
    private static ConnectionSigleton instance;
    Socket socket;
    InputStream is = null;
    InputStreamReader isr = null;
    BufferedReader br = null;
    OutputStream os = null;

    public ConnectionSigleton() {
        socket = null;
    }

    public void CreateSocket(String host, int port) throws IOException {
            socket = new Socket(host,port);
            is = socket.getInputStream();
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
            os = socket.getOutputStream();
    }
    
    public String readSocket() throws IOException{
        char chars[] = new char[2048];
        String x;
        int error = br.read(chars);
        if (error == -1){
            throw new IOException("Connection Closed");
        }else{
            x = new String(chars);
            System.out.println("MSG_RECV: "+x);
            return x;
        }
        
    }
    
    public void writeSocket(String buff)throws IOException{
         os.write(buff.getBytes("US-ASCII"));
         System.out.println("MSG_WRITE: "+buff);
    }
    
    public static ConnectionSigleton getInstance(){
        if(instance == null){
            instance = new ConnectionSigleton();
        }
        return instance;
        
    }
    
    public boolean is_connected(){
        return socket.isConnected();
    }
    
}
